package cn.hfatec.shms.web.qz.qzByl.action;

import cn.hfatec.JsonResponse;
import cn.hfatec.Page;
import cn.hfatec.Table;
import cn.hfatec.freemarker.FreeMarkerUtil;
import cn.hfatec.shms.base.ShmsBaseAction;
import cn.hfatec.shms.web.qz.qzByl.model.QzBylParams;
import cn.hfatec.shms.web.qz.qzByl.service.QzBylService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.List;
import java.util.Map;


/**
 * @author hfatec codegen
 * @version 1.0
 *          <p/>
 *          the JAVA code is generate by middlegen
 */
@Controller
@RequestMapping("/qz/qzByl")
public class QzBylAction extends ShmsBaseAction {

    //服务层
    @Resource(name = "QzBylService")
    private QzBylService qzBylService;


    /**
     * 查询列表
     */
    @RequestMapping("/list")
    public String list(QzBylParams qzBylParams, Page page, Model model) {
        page.setPageSize(17);
        Table table = qzBylService.queryQzBylTable(qzBylParams, page);
        List list = qzBylService.qzBylHz(qzBylParams);
        List listDif = qzBylService.qzBylDif(qzBylParams);
        Calendar c = Calendar.getInstance();
        int week = c.get(Calendar.WEEK_OF_YEAR);
        model.addAttribute("weekNum", week);
        model.addAttribute("qzBylsTable", table);
        model.addAttribute("qzBylsList", list);
        model.addAttribute("qzBylsDifList", listDif);
        model.addAttribute("qzBylParams", qzBylParams);
        return "/qz/qzByl/list";
    }

    /**
     * 异步查询列表
     */
    @RequestMapping("/ajaxList")
    @ResponseBody
    public String ajaxList(QzBylParams qzBylParams, Page page, Model model) {
        try {
            list(qzBylParams, page, model);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/qz/qzByl/table", model)).toString();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }


    /**
     * 跳转到选择产品权益页
     */
    @RequestMapping("/qzbylMxSelect")
    @ResponseBody
    public String qzbylMxSelect(QzBylParams qzBylParams, Page page, Model model) {

        try {
            list(qzBylParams, page, model);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/qz/qzByl/selectQzByl", model)).toString();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /*
         * 异步查询列表
         */
    @RequestMapping("/ajaxSelectList")
    @ResponseBody
    public String ajaxSelectList(QzBylParams qzBylParams, Model model, Page page) {
        try {
            list(qzBylParams, page, model);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/qz/qzByl/selectTable", model)).toString();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

}
